package top.luoqiz.jenkins.config.sms;

import org.springframework.lang.Nullable;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author luoqiz
 */
public class SmsVerificationCodeAuthenticationFilter extends
        AbstractAuthenticationProcessingFilter {
    // ~ Static fields/initializers
    // =====================================================================================

    public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "mobile";
    public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "code";

    private String mobileNumParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
    private String verificationCodeParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
    private boolean postOnly = true;

    // ~ Constructors
    // ===================================================================================================

    public SmsVerificationCodeAuthenticationFilter() {
        super(new AntPathRequestMatcher("/login/sms", "POST"));
    }

    // ~ Methods
    // ========================================================================================================

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
                                                HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

        String mobile = obtainMobileNum(request);
        String code = obtainVerificationCode(request);

        if (mobile == null) {
            mobile = "";
        }

        if (code == null) {
            code = "";
        }

        mobile = mobile.trim();

        SmsVerificationCodeAuthenticationToken authRequest = new SmsVerificationCodeAuthenticationToken(
                mobile, code);

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

        return this.getAuthenticationManager().authenticate(authRequest);
    }

    @Nullable
    protected String obtainVerificationCode(HttpServletRequest request) {
        return request.getParameter(verificationCodeParameter);
    }

    @Nullable
    protected String obtainMobileNum(HttpServletRequest request) {
        return request.getParameter(mobileNumParameter);
    }

    protected void setDetails(HttpServletRequest request,
                              SmsVerificationCodeAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

    public void setUsernameParameter(String mobileNumParameter) {
        Assert.hasText(mobileNumParameter, "Mobile number parameter must not be empty or null");
        this.mobileNumParameter = mobileNumParameter;
    }

    public void setPasswordParameter(String verificationCodeParameter) {
        Assert.hasText(verificationCodeParameter, "Verification code parameter must not be empty or null");
        this.verificationCodeParameter = verificationCodeParameter;
    }

    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }

    public final String getMobileNumParameter() {
        return mobileNumParameter;
    }

    public final String getVerificationCodeParameter() {
        return verificationCodeParameter;
    }
}
